import { Column, Entity, JoinColumn, OneToMany, TreeChildren, TreeParent } from 'typeorm';
import { BaseEntity } from './BaseEntity';
import { SysDepartmentRole } from './SysDepartmentRole';
import { SysDepartmentUser } from './SysDepartmentUser';

@Entity({
  orderBy: {
    sort: 'ASC',
    createTime: 'DESC'
  }
})
export class SysDepartment extends BaseEntity {
  @Column({
    type: 'varchar',
    comment: '名称'
  })
  title: string;

  @Column({
    type: 'boolean',
    comment: '是否禁用',
    default: false
  })
  forbidden: boolean;
  @Column({
    type: 'varchar',
    nullable: true,
    comment: '图标'
  })
  icon: string;

  @Column({
    type: 'boolean',
    comment: '是否叶子',
    name: 'is_leaf',
    default: false
  })
  isLeaf: boolean;
  @Column({
    type: 'varchar',
    nullable: true,
    comment: '备注'
  })
  remark: string;

  @Column({
    type: 'int',
    comment: '序号'
  })
  sort: number;

  @TreeChildren()
  children: SysDepartment[];

  @TreeParent({
    onDelete: 'CASCADE'
  })
  @JoinColumn({
    name: 'parent_id'
  })
  parent: SysDepartment;

  @OneToMany(() => SysDepartmentRole, s => s.SysDepartmentId)
  SysDepartmentRole: SysDepartmentRole[];

  @OneToMany(() => SysDepartmentUser, s => s.SysDepartmentId)
  SysDepartmentUser: SysDepartmentUser[];
}
